using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace Slaks.Web.Parser
{
  public class HtmlHeader
  {
    private StringCollection m_metaname = null;
    private string m_title = String.Empty;

    public HtmlHeader() {}

    /// <summary>
    /// Get or add metaname information
    /// </summary>
    public IList Metaname
    {
      get 
      { 
        if (m_metaname == null) m_metaname = new StringCollection();
        return m_metaname; 
      }
    }
    /// <summary>
    /// Get or Set Title of the page
    /// </summary>
    public string Title
    {
      get { return m_title; }
      set { m_title = value; }
    }

    /// <summary>
    /// Extract the title from the provided text
    /// </summary>
    /// <param name="text"></param>
    internal void ExtractTitle(string text)
    {
      System.Text.StringBuilder title = new System.Text.StringBuilder();
      for (int i=1;i<text.Length;i++)
      {
        if (text[i] == '<') break;
        title.Append(char.ToLower(text[i]));
      }
      
      this.Title = DocumentHtmlParser.ScanTextForSpecialEntityCharacters(title.ToString());
    }


    /// <summary>
    /// Extract the meta name information from the header
    /// </summary>
    /// <param name="text"></param>
    internal bool ExtractMetaName(string text)
    {
      string pattern = @"name+.*?content+\s*=\s*[""']?([^'""]+?)['""]\.*?";
      Match m = Regex.Match(text,pattern,RegexOptions.IgnoreCase);
      if (m.Success) 
      {
        GroupCollection gc = m.Groups;
        Group g = gc[1];
        if (g.Value != String.Empty) this.Metaname.Add(DocumentHtmlParser.ScanTextForSpecialEntityCharacters(g.Value));
      }
      return m.Success;
    }
  }
}
